package org.misalen.web.admin.controllers;

import java.util.Date;

import org.misalen.web.admin.util.ServerResult;
import org.misalen.web.database.model.mysql.pro.ProDriverUserExtractCash;
import org.misalen.web.database.repository.mysql.ProDriverUserExtractCashRepository;
import org.misalen.web.database.util.PaginationFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "/pro-driver-user-extract")
public class ProDriverUserExtractCashController extends BaseController {

	@Autowired
	protected ProDriverUserExtractCashRepository proDriverUserExtractCashRepository;

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/apply/", method = { RequestMethod.GET })
	public ModelAndView list1() {
		ModelAndView modelAndView = new ModelAndView("/proDriverUserExtractCash/apply/list");
		return modelAndView;
	}

	/**
	 * 列表
	 * 
	 * @param 搜索条件
	 * @param 分页/排序条件
	 * @return 查询结果
	 */
	@RequestMapping(value = "/apply/", method = { RequestMethod.POST })
	public @ResponseBody ServerResult listPost(PaginationFrom<ProDriverUserExtractCash> pagination) {
		PaginationFrom<ProDriverUserExtractCash> from = proDriverUserExtractCashRepository
				.findPageByApply("not-operating", getShiroUser().getBelongCity(), pagination);
		return renderSuccess(from);
	}

	/**
	 * 列表页
	 * 
	 * @return 列表目标页
	 */
	@RequestMapping(value = "/cash/", method = { RequestMethod.GET })
	public ModelAndView list2() {
		ModelAndView modelAndView = new ModelAndView("/proDriverUserExtractCash/cash/list");
		return modelAndView;
	}

	/**
	 * 列表
	 * 
	 * @param 搜索条件
	 * @param 分页/排序条件
	 * @return 查询结果
	 */
	@RequestMapping(value = "/cash/", method = { RequestMethod.POST })
	public @ResponseBody ServerResult listPost2(PaginationFrom<ProDriverUserExtractCash> pagination, String state) {
		if (state == null || state.equals("")) {
			state = "complete";
		}
		ProDriverUserExtractCash proDriverUserExtractCash = new ProDriverUserExtractCash();
		proDriverUserExtractCash.setState(state);
		PaginationFrom<ProDriverUserExtractCash> from = proDriverUserExtractCashRepository.findPageByApply(state, getShiroUser().getBelongCity(),
				pagination);
		return renderSuccess(from);
	}

	@RequestMapping(value = "/apply/complete/{primaryKey}", method = { RequestMethod.GET })
	public @ResponseBody ServerResult complete(@PathVariable String primaryKey) {
		ProDriverUserExtractCash driverUserExtractCash = proDriverUserExtractCashRepository
				.get(ProDriverUserExtractCash.class, primaryKey);
		driverUserExtractCash.setState("complete");
		driverUserExtractCash.setProcessingTime(new Date());
		proDriverUserExtractCashRepository.update(driverUserExtractCash);
		proDriverUserRepository.updateWithDraw(driverUserExtractCash.getAmount().intValue(), primaryKey);
		
		return ServerResult.ok();
	}

	@RequestMapping(value = "/apply/refuse/{primaryKey}", method = { RequestMethod.GET })
	public @ResponseBody ServerResult refuse(@PathVariable String primaryKey) {
		ProDriverUserExtractCash driverUserExtractCash = proDriverUserExtractCashRepository
				.get(ProDriverUserExtractCash.class, primaryKey);
		driverUserExtractCash.setState("refuse");
		driverUserExtractCash.setProcessingTime(new Date());
		proDriverUserExtractCashRepository.update(driverUserExtractCash);
		proDriverUserRepository.updateBalance(driverUserExtractCash.getAmount().intValue(), driverUserExtractCash.getDriverUserId());
		return ServerResult.ok();
	}
}
